<template>
  <el-dialog
    title="设置可见范围"
    :close-on-click-modal="false"
    append-to-body
    :visible.sync="visible"
    class="jsbos-dialog jsbos-dialog_center"
    lock-scroll
    width="800px"
    style="min-height: 250px"
  >
    <el-row
      :gutter="15"
      class="main"
      :style="{ margin: '0 auto', width: '800px' }"
    >
      <el-form
        ref="elForm"
        :model="dataForm"
        :rules="rules"
        size="small"
        label-width="100px"
        label-position="right"
      >
        <template v-if="!loading">
          <el-col :span="24">
            <el-col :span="24">
              <el-form-item label-width="0">
                <groupTitle content-position="left" content="可见范围">
                </groupTitle>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="可见范围" prop="scope">
                <el-radio-group v-model="dataForm.scopeType" size="small">
                  <el-radio
                    v-for="(item, index) in scopeOptions"
                    :key="index"
                    :label="item.id"
                    :disabled="item.disabled"
                  >
                    {{ item.fullName }}
                  </el-radio>
                </el-radio-group>
                <filterSelect
                  v-if="dataForm.scopeType == 'select'"
                  width="500px"
                  v-model="dataForm.scope"
                  @change="getScopeChangeData"
                ></filterSelect>
              </el-form-item>
            </el-col>
          </el-col>
        </template>
      </el-form>
    </el-row>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false"> 取 消</el-button>
      <el-button type="primary" @click="dataFormSubmit()" v-if="!isDetail">
        确 定</el-button
      >
    </span>
  </el-dialog>
</template>
<script>
import request from "@/utils/request";
import FilterSelect from "@/components/Generator/components/filterSelect/index";

export default {
  components: { FilterSelect },
  props: [],
  data() {
    return {
      visible: false,
      loading: false,
      isDetail: false,
      dataForm: {
        scopeType: "all",
        scope: [],
      },
      rules: {},
      labelOptions: [],
      scopeOptions: [
        { fullName: "全公司", id: "all" },
        { fullName: "仅创建人", id: "creator" },
        {
          fullName: "指定人员",
          id: "select",
        },
      ],
    };
  },
  computed: {},
  watch: {},
  created() {},
  mounted() {},
  methods: {
    getScopeChangeData(scopeData) {
      this.dataForm.scope = scopeData;
    },
    goBack() {
      this.$emit("refresh");
    },
    clearData(data) {
      for (let key in data) {
        if (data[key] instanceof Array) {
          data[key] = [];
        } else if (data[key] instanceof Object) {
          this.clearData(data[key]);
        } else {
          data[key] = "";
        }
      }
      this.dataForm.scope = [];
      this.dataForm.scopeType = "all";
    },
    init(id) {
      this.dataForm.id = id || 0;
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["elForm"].resetFields();
        if (this.dataForm.id) {
          this.loading = true;
          request({
            url: "/api/course/TemsCourse/" + this.dataForm.id,
            method: "get",
          }).then((res) => {
            this.dataInfo(res.data);
            this.loading = false;
          });
        } else {
          this.clearData(this.dataForm);
        }
      });
      this.$store.commit("generator/UPDATE_RELATION_DATA", {});
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["elForm"].validate((valid) => {
        if (valid) {
          this.request();
        }
      });
    },
    request() {
      var _data = this.dataList();
      request({
        url: "/api/course/TemsCourse/updateScope/" + this.dataForm.id,
        method: "PUT",
        data: _data,
      }).then((res) => {
        this.$message({
          message: res.msg,
          type: "success",
          duration: 1000,
          onClose: () => {
            this.visible = false;
            this.$emit("refresh", true);
          },
        });
      });
    },
    scopeFormat(data) {
      let scope = {};
      let organizesArr = [];
      data.forEach((item, index) => {
        if (item.type == "company" || item.type == "department") {
          organizesArr = organizesArr.concat(item.selectArr);
        }
        if (item.type == "Role") {
          scope.scopeRoles = item.selectArr.join(",");
        }
        if (item.type == "Position") {
          scope.scopePositions = item.selectArr.join(",");
        }
        if (item.type == "Group") {
          scope.scopeGroups = item.selectArr.join(",");
        }
        if (item.type == "user") {
          scope.scopeUsers = item.selectArr.join(",");
        }
        if (item.type == "exclude") {
          scope.scopeExcludeUsers = item.selectArr.join(",");
        }
      });
      scope.scopeOrganizes = organizesArr.join(",");
      return scope;
    },
    dataList() {
      var _data = JSON.parse(JSON.stringify(this.dataForm));
      let scopeFormatProperties = this.scopeFormat(_data.scope);
      _data.scope = JSON.stringify(_data.scope);
      _data.scopeOrganizes = scopeFormatProperties.scopeOrganizes;
      _data.scopeGroups = scopeFormatProperties.scopeGroups;
      _data.scopeRoles = scopeFormatProperties.scopeRoles;
      _data.scopePositions = scopeFormatProperties.scopePositions;
      _data.scopeUsers = scopeFormatProperties.scopeUsers;
      _data.scopeExcludeUsers = scopeFormatProperties.scopeExcludeUsers;
      return _data;
    },
    dataInfo(dataAll) {
      let _dataAll = dataAll;
      _dataAll.scope = JSON.parse(_dataAll.scope);
      this.dataForm = _dataAll;
    },
  },
};
</script>
